//###############
//FILE : WantAllFilesRequest.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.nameserver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import oop.ex3.protocol.sessions.NameWantAllFilesSession;

/**
 * This handles the WANTALLFILES request as specified in the assignment.
 * @author daniel
 */
public class WantAllFilesRequest extends FileManagerSpecificRequest{
    private NameWantAllFilesSession _wantFilesSession;

    /**
     * Create a new request initialized with a NS database which is passed to the super class.
     * The opening session is a NameWantAllFilesSession.
     * @param nsDb The nameserver database.
     */
    public WantAllFilesRequest(NameServerDatabase nsDb) {
        super(nsDb, new NameWantAllFilesSession());
        _wantFilesSession = (NameWantAllFilesSession)_openingSession;
    }

    @Override
    public boolean serveRequest(DataInputStream inputStream, DataOutputStream outputStream) {
        //System.out.println("serving want all files request");

        //first run the want all files session
        _wantFilesSession.setFileNames(_nameServerDb.getSortedAllFiles());

        if (!_wantFilesSession.executeSession(inputStream, outputStream)) {
            return false;
        }


        //if we didn't find the end  then there was an error
        if (!_endSession.executeSession(inputStream, outputStream)) {
            return false;
        }

        return true;
    }

    @Override
    public NameServerRequest getCopy() {
        WantAllFilesRequest copy = new WantAllFilesRequest(_nameServerDb);
        return copy;
    }

}
